/*
 * @Author: your name
 * @Date: 2020-04-17 17:02:40
 * @LastEditTime: 2020-06-03 18:45:32
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \v-nw-h5\src\utils\StringUtils.js
 */
export default {
    isBlank (obj) {
        return !obj;
    },

    isNotBlank (obj) {
        return !!obj;
    },
    /**
     * 替换指定传入参数的值,paramName为参数,replaceWith为新值
     * @param paramName
     * @param replaceWith
     */
    replaceParamVal (url, paramName, replaceWith) {
        var re = eval('/(' + paramName + '=)([^&]*)/gi');
        var nUrl = url.replace(re, paramName + '=' + replaceWith);
        return nUrl;
    },
    /**
     * 获取URL参数 例：var nameVal=GetQueryString("URL上的参数名");
     * @param name
     * @returns {null}
     * @constructor
     */
    GetQueryString (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    },
    randomStr (length) {
        return Math.random().toString(36).substr(2).slice(1, 1 + length)
    },
    /**
     * 删除URL中的指定参数
     * @param {*} url 
     * @param {*} name 
     */
    delUrlParams (url, name) {
        var urlArr = url.split('?');
        if (urlArr.length > 1 && urlArr[1].indexOf(name) > -1) {
            var query = urlArr[1];
            var obj = {}
            var arr = query.split("&");
            for (var i = 0; i < arr.length; i++) {
                arr[i] = arr[i].split("=");
                obj[arr[i][0]] = arr[i][1];
            };
            delete obj[name];
            var urlte = urlArr[0] + '?' + JSON.stringify(obj).replace(/[\"\{\}]/g, "").replace(/\:/g, "=").replace(/\,/g, "&");
            return urlte;
        } else {
            return url;
        };
    },
    sha1(s) {
        var data = new Uint8Array(this.encodeUTF8(s))
        var i, j, t;
        var l = ((data.length + 8) >>> 6 << 4) + 16, s = new Uint8Array(l << 2);
        s.set(new Uint8Array(data.buffer)), s = new Uint32Array(s.buffer);
        for (t = new DataView(s.buffer), i = 0; i < l; i++) s[i] = t.getUint32(i << 2);
        s[data.length >> 2] |= 0x80 << (24 - (data.length & 3) * 8);
        s[l - 1] = data.length << 3;
        var w = [], f = [
                function () {
                    return m[1] & m[2] | ~m[1] & m[3];
                },
                function () {
                    return m[1] ^ m[2] ^ m[3];
                },
                function () {
                    return m[1] & m[2] | m[1] & m[3] | m[2] & m[3];
                },
                function () {
                    return m[1] ^ m[2] ^ m[3];
                }
            ], rol = function (n, c) {
                return n << c | n >>> (32 - c);
            },
            k = [1518500249, 1859775393, -1894007588, -899497514],
            m = [1732584193, -271733879, null, null, -1009589776];
        m[2] = ~m[0], m[3] = ~m[1];
        for (i = 0; i < s.length; i += 16) {
            var o = m.slice(0);
            for (j = 0; j < 80; j++)
                w[j] = j < 16 ? s[i + j] : rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1),
                    t = rol(m[0], 5) + f[j / 20 | 0]() + m[4] + w[j] + k[j / 20 | 0] | 0,
                    m[1] = rol(m[1], 30), m.pop(), m.unshift(t);
            for (j = 0; j < 5; j++) m[j] = m[j] + o[j] | 0;
        }
        ;
        t = new DataView(new Uint32Array(m).buffer);
        for (var i = 0; i < 5; i++) m[i] = t.getUint32(i << 2);

        var hex = Array.prototype.map.call(new Uint8Array(new Uint32Array(m).buffer), function (e) {
            return (e < 16 ? "0" : "") + e.toString(16);
        }).join("");
        return hex;
    },
    encodeUTF8(text) {
        const code = encodeURIComponent(text);
        const bytes = [];
        for (var i = 0; i < code.length; i++) {
            const c = code.charAt(i);
            if (c === '%') {
                const hex = code.charAt(i + 1) + code.charAt(i + 2);
                const hexVal = parseInt(hex, 16);
                bytes.push(hexVal);
                i += 2;
            } else bytes.push(c.charCodeAt(0));
        }
        return bytes;
    }
}
